﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CYZH.Common
{
    // Define a custom section.
    // The CustomSection type allows to define a custom section 
    // programmatically.
    public class CustomSection : ConfigurationSection
    {
        [ConfigurationProperty("OutRaster", IsDefaultCollection = false)]
        public OutRasterCollection OutRaster
        {
            get { return (OutRasterCollection)this["OutRaster"]; }
            set { this["OutRaster"] = value; }

        }
        [ConfigurationProperty("AscSetting")]
        public AscSettingElements AscSetting
        {
            get { return (AscSettingElements)this["AscSetting"]; }
            set { this["AscSetting"] = value; }

        }
    }
   
    // Define the UrlsConfigElement elements that are contained 
    // by the UrlsCollection.
    public class AscSettingElements : ConfigurationElement
    {
        [ConfigurationProperty("minx",
            IsRequired = true, IsKey = true)]
        public string MinX
        {
            get
            {
                return (string)this["minx"];
            }
            set
            {
                this["minx"] = value;
            }
        }

        [ConfigurationProperty("miny", 
            IsRequired = true)]
        public string MinY
        {
            get
            {
                return (string)this["miny"];
            }
            set
            {
                this["miny"] = value;
            }
        }
        [ConfigurationProperty("maxx",
            IsRequired = true, IsKey = true)]
        public string MaxX
        {
            get
            {
                return (string)this["maxx"];
            }
            set
            {
                this["maxx"] = value;
            }
        }

        [ConfigurationProperty("maxy",
            IsRequired = true)]
        public string MaxY
        {
            get
            {
                return (string)this["maxy"];
            }
            set
            {
                this["maxy"] = value;
            }
        }

        [ConfigurationProperty("rows", IsRequired = true)]
        
        public int Rows
        {
            get
            {
                return (int)this["rows"];
            }
            set
            {
                this["rows"] = value;
            }
        }
        [ConfigurationProperty("cols", IsRequired = true)]

        public int Cols
        {
            get
            {
                return (int)this["cols"];
            }
            set
            {
                this["cols"] = value;
            }
        }
        [ConfigurationProperty("cellsize",
           IsRequired = true, IsKey = true)]
        public string CellSize
        {
            get
            {
                return (string)this["cellsize"];
            }
            set
            {
                this["cellsize"] = value;
            }
        }

    }
    public class OutRasterCollection : ConfigurationElementCollection
    {


        public OutRasterCollection()
        {

        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new OutRasterConfigElement();
        }

        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((OutRasterConfigElement)element).Name;
        }

        public OutRasterConfigElement this[int index]
        {
            get
            {
                return (OutRasterConfigElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public OutRasterConfigElement this[string Name]
        {
            get
            {
                return (OutRasterConfigElement)BaseGet(Name);
            }
        }


        public int IndexOf(OutRasterConfigElement url)
        {
            return BaseIndexOf(url);
        }

        public void Add(OutRasterConfigElement url)
        {
            BaseAdd(url);

            // Your custom code goes here.

        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);

            // Your custom code goes here.

        }

        public void Remove(OutRasterConfigElement url)
        {
            if (BaseIndexOf(url) >= 0)
            {
                BaseRemove(url.Name);
                // Your custom code goes here.
                Console.WriteLine("UrlsCollection: {0}", "Removed collection element!");
            }
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);

            // Your custom code goes here.

        }

        public void Remove(string name)
        {
            BaseRemove(name);

            // Your custom code goes here.

        }

        public void Clear()
        {
            BaseClear();

            // Your custom code goes here.
            Console.WriteLine("UrlsCollection: {0}", "Removed entire collection!");
        }

    }
    // Define the UrlsConfigElement elements that are contained 
    // by the UrlsCollection.
    public class OutRasterConfigElement : ConfigurationElement
    {
        public OutRasterConfigElement(String name, String code, int rows, int cols, String extents,String shpfile,String childShpFile)
        {
            this.Name = name;
            this.Code = code;
            this.Rows = rows;
            this.Cols = cols;
            this.Extents = extents;
            this.ShpFile=shpfile;
            this.ChildShpFile = childShpFile;
        }

        public OutRasterConfigElement()
        {

        }

        [ConfigurationProperty("name",
            IsRequired = true, IsKey = true)]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("code",
            IsRequired = true, IsKey = true)]

        public string Code
        {
            get
            {
                return (string)this["code"];
            }
            set
            {
                this["code"] = value;
            }
        }

        [ConfigurationProperty("rows", IsRequired = false)]
        public int Rows
        {
            get
            {
                return (int)this["rows"];
            }
            set
            {
                this["rows"] = value;
            }
        }
        [ConfigurationProperty("cols", IsRequired = false)]
        public int Cols
        {
            get
            {
                return (int)this["cols"];
            }
            set
            {
                this["cols"] = value;
            }
        }
        [ConfigurationProperty("extents",
            IsRequired = true, IsKey = true)]
        public string Extents
        {
            get
            {
                return (string)this["extents"];
            }
            set
            {
                this["extents"] = value;
            }
        }
        [ConfigurationProperty("shpfile",
           IsRequired = true, IsKey = true)]
        public string ShpFile
        {
            get
            {
                return (string)this["shpfile"];
            }
            set
            {
                this["shpfile"] = value;
            }
        }
        [ConfigurationProperty("childShpFile",
           IsRequired = true, IsKey = true)]
        public string ChildShpFile
        {
            get
            {
                return (string)this["childShpFile"];
            }
            set
            {
                this["childShpFile"] = value;
            }
        }
    }
}
